package com.example.myapplicationwork;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;

public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";
    private static final String ARG_INTEREST = "interest";
    private String currentInterest;
    private TextView tvRecommendation;

    // 接口用于与宿主Activity通信
    public interface OnInterestUpdatedListener {
        void onInterestUpdated(String interest);
    }

    // 创建Fragment实例的静态方法
    public static HomeFragment newInstance(String interest) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_INTEREST, interest);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        if (getArguments() != null) {
            currentInterest = getArguments().getString(ARG_INTEREST, "未选择");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        tvRecommendation = view.findViewById(R.id.tv_interest_recommendation);
        updateInterestDisplay();
        return view;
    }

    // 更新兴趣显示
    private void updateInterestDisplay() {
        if (tvRecommendation != null) {
            tvRecommendation.setText("根据您的兴趣【" + currentInterest + "】，推荐内容如下");
        }
    }

    // 供宿主调用的更新兴趣方法
    public void updateInterest(String interest) {
        currentInterest = interest;
        updateInterestDisplay();
    }

    // 生命周期方法
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}
